package org.dromara.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.DefaultExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.system.domain.bo.WbItemInfoBo;
import org.dromara.system.domain.dr.WbMinuteBidBagDR;
import org.dromara.system.domain.dto.WbMinuteBidBagDTO;
import org.dromara.system.domain.dto.WbMinuteBidBagSiftDTO;
import org.dromara.system.domain.vo.*;
import org.dromara.system.listener.WbCandidateInfoListener;
import org.dromara.system.listener.WbGoodsListInfoImportListener;
import org.dromara.system.listener.WbMinuteBidBagHistoryImportListener;
import org.dromara.system.listener.WbMinuteBidBagImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.bo.WbMinuteBidBagBo;
import org.dromara.system.service.IWbMinuteBidBagService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 中标结果公告
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/minuteBidBag")
public class WbMinuteBidBagController extends BaseController {

    private final IWbMinuteBidBagService wbMinuteBidBagService;

    /**
     * 查询中标结果公告列表
     */
    @SaCheckPermission("system:minuteBidBag:list")
    @GetMapping("/list")
    public TableDataInfo<WbMinuteBidBagVo> list(WbMinuteBidBagBo bo, PageQuery pageQuery) {
        return wbMinuteBidBagService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出中标结果公告列表
     */
    @SaCheckPermission("system:minuteBidBag:export")
    @Log(title = "中标结果公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbMinuteBidBagBo bo, HttpServletResponse response) {
        List<WbMinuteBidBagVo> list = wbMinuteBidBagService.queryList(bo);
        ExcelUtil.exportExcel(list, "中标结果公告", WbMinuteBidBagVo.class, response);
    }

    /**
     * 导出中标结果公告列表
     */
    @SaCheckPermission("system:minuteBidBag:export")
    @Log(title = "中标结果公告", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate( HttpServletResponse response) {
        WbMinuteBidBagVo wbMinuteBidBagVo = new WbMinuteBidBagVo();
        List<WbMinuteBidBagVo> list = new ArrayList<>();
        wbMinuteBidBagVo.setBidDate("xxxx年XX月XX日");
        wbMinuteBidBagVo.setTenderee("xxxxxx公司");
        wbMinuteBidBagVo.setTenderingAgency("XXXXXXXXXX公司");
        wbMinuteBidBagVo.setItemName("XXXXXXXXX公告");
        wbMinuteBidBagVo.setItemId("12314");
        list.add(wbMinuteBidBagVo);
        ExcelUtil.exportExcel(list, "中标结果公告", WbMinuteBidBagVo.class, response);
    }

    /**
     * 导入数据
     * @param file          导入文件
     */
    @Log(title = "中标结果公告", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:minuteBidBag:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<WbMinuteBidBagVo> result = ExcelUtil.importExcel(file.getInputStream(), WbMinuteBidBagVo.class,new WbMinuteBidBagImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 导入历史数据数据
     * @param file          导入文件
     */
    @Log(title = "中标结果公告", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:minuteBidBag:add")
    @PostMapping(value = "/importDataHistory", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importDataHistory(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<WbMinuteBidBagHistoryVo> result = ExcelUtil.importExcel(file.getInputStream(), WbMinuteBidBagHistoryVo.class,new WbMinuteBidBagHistoryImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 下载历史模板
     * @param response
     */
    @SaCheckPermission("system:minuteBidBag:export")
    @Log(title = "中标结果公告", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplateHistory")
    public void exportTemplateHistory( HttpServletResponse response) {
        WbMinuteBidBagHistoryVo wbMinuteBidBagHistoryVo = new WbMinuteBidBagHistoryVo();
        List<WbMinuteBidBagHistoryVo> list = new ArrayList<>();
        wbMinuteBidBagHistoryVo.setItemName("XX项目名称");
        wbMinuteBidBagHistoryVo.setSubStandardName("XX产品名称/分标名称");
        wbMinuteBidBagHistoryVo.setYear("2024");
        wbMinuteBidBagHistoryVo.setPrice("1.16（注意单位万元）");
        wbMinuteBidBagHistoryVo.setProvince("江西省");
        wbMinuteBidBagHistoryVo.setBagNo("包X");
        wbMinuteBidBagHistoryVo.setCompanyName("XX公司");
        list.add(wbMinuteBidBagHistoryVo);
        ExcelUtil.exportExcel(list, "中标结果公告", WbMinuteBidBagHistoryVo.class, response);
    }

    @SaCheckPermission("system:companyBlacklist:updateAllList")
    @Log(title = "中标结果公告")
    @PostMapping(value = "/updateAllList")
    public R importList(@RequestParam("file") MultipartFile file, boolean updateSupport) throws IOException {
        ExcelResult<WbMinuteBidBagDR> result = ExcelUtil.importExcel(file.getInputStream(), WbMinuteBidBagDR.class, new DefaultExcelListener(true));
        return wbMinuteBidBagService.importList(result);
    }

    /**
     * 获取中标结果公告详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:minuteBidBag:query")
    @GetMapping("/{id}")
    public R<WbMinuteBidBagVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbMinuteBidBagService.queryById(id));
    }

    /**
     * 新增中标结果公告
     */
    @SaCheckPermission("system:minuteBidBag:add")
    @Log(title = "中标结果公告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbMinuteBidBagBo bo) {
        if(StringUtils.isNotEmpty(bo.getBidDate())){
            bo.setYear(bo.getBidDate().substring(0,4));
        }
        return toAjax(wbMinuteBidBagService.insertByBo(bo));
    }

    /**
     * 修改中标结果公告
     */
    @SaCheckPermission("system:minuteBidBag:edit")
    @Log(title = "中标结果公告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbMinuteBidBagBo bo) {
        if(StringUtils.isNotEmpty(bo.getBidDate())){
            bo.setYear(bo.getBidDate().substring(0,4));
        }
        return toAjax(wbMinuteBidBagService.updateByBo(bo));
    }

    /**
     * 批量发布项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:itemInfo:edit")
    @Log(title = "中标结果公告", businessType = BusinessType.UPDATE)
    @PutMapping("/release/{ids}")
    public R<Void> release(@NotEmpty(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        for (Long id : ids) {
            WbMinuteBidBagVo wbMinuteBidBagVo = wbMinuteBidBagService.queryById(id);
            if("-1".equals(wbMinuteBidBagVo.getStatus())){
                wbMinuteBidBagVo.setStatus("0");
                wbMinuteBidBagService.updateByBo(BeanUtil.toBean(wbMinuteBidBagVo, WbMinuteBidBagBo.class));
            }
        }
        return R.ok();
    }

    /**
     * 批量撤回项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:itemInfo:edit")
    @Log(title = "中标结果公告", businessType = BusinessType.UPDATE)
    @PutMapping("/withdraw/{ids}")
    public R<Void> withdraw(@NotEmpty(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        for (Long id : ids) {
            WbMinuteBidBagVo wbMinuteBidBagVo = wbMinuteBidBagService.queryById(id);
            if(!"-1".equals(wbMinuteBidBagVo.getStatus())){
                wbMinuteBidBagVo.setStatus("-1");
                wbMinuteBidBagService.updateByBo(BeanUtil.toBean(wbMinuteBidBagVo, WbMinuteBidBagBo.class));
            }
        }
        return R.ok();
    }

    /**
     * 删除中标结果公告
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:minuteBidBag:remove")
    @Log(title = "中标结果公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbMinuteBidBagService.deleteWithValidByIds(List.of(ids), true));
    }


    @GetMapping("/listSift")
    public R<WbMinuteBidBagSiftDTO> listSift(WbMinuteBidBagBo bo, PageQuery pageQuery) {
        return wbMinuteBidBagService.queryListSift(bo, pageQuery);
    }

}
